package com.zhilianbao.leyaogo.ui.adapter.me;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bql.recyclerview.swipe.SwipeMenuAdapter;
import com.bql.roundview.RoundTextView;
import com.bql.utils.CheckUtils;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.listener.OnAddToCartClickListener;
import com.zhilianbao.leyaogo.model.response.me.CouponCanUsedGoodsResponse;
import com.zhilianbao.leyaogo.utils.ImageUtils;
import com.zhilianbao.leyaogo.utils.Utils;
import com.zhilianbao.leyaogo.view.widgets.MoneyTextView;

import java.util.List;

/**
 * Created by liu on 2016/10/29.
 * 适用商品adapter
 */
public class CouponCanUsedAdapter extends SwipeMenuAdapter<CouponCanUsedAdapter.MyCouponCanUsedViewHolder> {
    private List<CouponCanUsedGoodsResponse> mList;
    private Context mContext;

    public CouponCanUsedAdapter(Context context, List<CouponCanUsedGoodsResponse> list) {
        mList = list;
        this.mContext = context;
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_collection, parent, false);
    }

    @Override
    public MyCouponCanUsedViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new MyCouponCanUsedViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(MyCouponCanUsedViewHolder holder, int position) {
        holder.setData(mContext, mOnAddToCartClickListener, mList.get(position));

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    static class MyCouponCanUsedViewHolder extends RecyclerView.ViewHolder {

        private MoneyTextView mAccountPrice;
        private ImageView mIvAdd;
        private OnAddToCartClickListener mOnAddToCartClickListener;
        private ImageView mProductDetailIcon;
        private TextView mGoodsTitle;
        private TextView mMGoodsDesc;
        private final RoundTextView mRvLable;

        public MyCouponCanUsedViewHolder(View itemView) {
            super(itemView);
            mAccountPrice = (MoneyTextView) itemView.findViewById(R.id.tv_account_price);//会员价
            mIvAdd = (ImageView) itemView.findViewById(R.id.iv_add);
            mProductDetailIcon = (ImageView) itemView.findViewById(R.id.product_detail_icon);
            mGoodsTitle = (TextView) itemView.findViewById(R.id.tv_goods_title);
            mMGoodsDesc = (TextView) itemView.findViewById(R.id.tv_goods_desc);
            mRvLable = (RoundTextView) itemView.findViewById(R.id.rv_label);
        }

        public void setData(Context context, OnAddToCartClickListener onAddToCartClickListener, CouponCanUsedGoodsResponse couponCanUsedGoodsResponse) {
            mOnAddToCartClickListener = onAddToCartClickListener;
            ImageUtils.loadGoodsPic(Utils.returnUrlShow(couponCanUsedGoodsResponse.getGoodsPic()), mProductDetailIcon);
            mGoodsTitle.setText(couponCanUsedGoodsResponse.getGoodsName());
            mMGoodsDesc.setText(couponCanUsedGoodsResponse.getGoodsDesc());
            mAccountPrice.setAmount(couponCanUsedGoodsResponse.getPrice());
            if (!CheckUtils.isEmpty(couponCanUsedGoodsResponse.getGoodsActivitySign())) {
                mRvLable.setVisibility(View.VISIBLE);
                mRvLable.setText(couponCanUsedGoodsResponse.getGoodsActivitySign());
            } else {
                mRvLable.setVisibility(View.GONE);
            }
            mIvAdd.setOnClickListener(v -> {
                if (mOnAddToCartClickListener != null) {
                    mOnAddToCartClickListener.onAddToCartClick(couponCanUsedGoodsResponse, mProductDetailIcon);
                }
            });
        }


    }

    private OnAddToCartClickListener mOnAddToCartClickListener;

    public void setOnAddCartClickListener(OnAddToCartClickListener mOnAddToCartClickListener) {
        this.mOnAddToCartClickListener = mOnAddToCartClickListener;
    }

}